var battle = require('./battle');
function handleProtocol(connection, socket, clientInfo) {
    switch (clientInfo.protocol) {
        case 10000://返回玩家信息
            pro10000(connection, socket, clientInfo);
            break;
        case 10001://创建角色
            pro10001(connection, socket, clientInfo);
            break;
        case 10002://玩家登录
            pro10002(connection, socket, clientInfo);
            break;
        case 10003://战斗
            pro10003(connection, socket, clientInfo);
            break;
        case 10004://更改属性点
            pro10004(connection, socket, clientInfo);
            break;
        case 10005://获取物品列表
            pro10005(connection, socket, clientInfo);
            break;
    }
}
function pro10000(connection, socket, clientInfo) {
    var select = 'select * from player where playerid="' + clientInfo.data.playerid + '" limit 1';
    connection.query(select, function (err, rows) {
        if (err) {
            console.log('select err 10000' + err);
            return;
        }
        var protocolInfo = {protocol: clientInfo.protocol, data: rows[0]};
        sendData(socket, protocolInfo);
    });
}
function pro10001(connection, socket, clientInfo) {
    var playerpoint = 5 - clientInfo.data.playerpower - clientInfo.data.playerintelligence - clientInfo.data.playernc - clientInfo.data.playergay;
    var insert = 'insert into ' + 'player(playerid,playername,playerlv,playerexperience,playerbyte,playerhealth,playerpower,playerintelligence,playernc,playergay,playerpoint) ' + 'values("' + clientInfo.data.playerid + '","' + clientInfo.data.playername + '",1,0,0,0,' + clientInfo.data.playerpower + ',' + clientInfo.data.playerintelligence + ',' + clientInfo.data.playernc + ',' + clientInfo.data.playergay + ',' + playerpoint + ')';
    connection.query(insert, function (err, res) {
        if (err) {
            if (err.code == 'ER_DUP_ENTRY') {
                console.log('insert err 10001' + err);
                var protocolInfo = {protocol: clientInfo.protocol, data: {result: 2, msg: '角色名已存在'}};
            }
            else {
                console.log('insert err 10001' + err);
                var protocolInfo = {protocol: clientInfo.protocol, data: {result: 0, msg: '创建失败'}};
            }
        }
        else
            var protocolInfo = {protocol: clientInfo.protocol, data: {result: 1, msg: '创建成功'}};
        sendData(socket, protocolInfo);
    });
}
function pro10002(connection, socket, clientInfo) {
    var select = 'select playername from player where playerid="' + clientInfo.data.playerid + '" limit 1';
    connection.query(select, function (err, rows) {
        if (err) {
            console.log('select err 10002' + err);
            var protocolInfo = {protocol: clientInfo.protocol, data: {result: 0, msg: '登录失败'}};
        }
        else if (rows.length == 0)
            var protocolInfo = {
                protocol: clientInfo.protocol,
                data: {result: 2, msg: '需要创建', playerpoint: 5}
            };
        else
            var protocolInfo = {protocol: clientInfo.protocol, data: {result: 1, msg: '可以登录'}};
        sendData(socket, protocolInfo);
    });
}
function pro10003(connection, socket, clientInfo) {
    var select = 'select * from player where playerid="' + clientInfo.data.playerid1 + '" limit 1';
    var select2 = 'select * from player where playerid="' + clientInfo.data.playerid2 + '" limit 1';
    var player1;
    var player2;
    connection.query(select, function (err, rows) {
        if (err) {
            console.log('insert err 10003' + err);
            notReady();
        }
        if (rows.length == 0)
            notReady();
        else {
            player1 = rows[0];
            if (player2 != null)
                allReady()
        }
    });
    connection.query(select2, function (err, rows) {
        if (err) {
            console.log('insert err 10003' + err);
            notReady();
        }
        if (rows.length == 0)
            notReady();
        else {
            player2 = rows[0];
            if (player1 != null)
                allReady();
        }
    });
    function allReady() {
        var battleData = [];
        battleData = battle.start([player1, player2]);
        var protocolInfo = {
            protocol: clientInfo.protocol,
            data: {playerinfo1: player1, playerinfo2: player2, result: 1, msg: '请求战斗成功', battleData: battleData}
        };
        sendData(socket, protocolInfo);
        return;
    }

    function notReady() {
        var protocolInfo = {protocol: clientInfo.protocol, data: {result: 0, msg: '请求战斗失败'}};
        sendData(socket, protocolInfo);
        return;
    }
}
function pro10004(connection, socket, clientInfo) {
    var playerInfo = getPlayer(connection, clientInfo.data.playerid);
    if (playerInfo == null) {
        var protocolInfo = {protocol: clientInfo.protocol, data: {result: 2, msg: '获取玩家信息失败'}};
        sendData(socket, protocolInfo);
        return;
    }
    var VHealth = clientInfo.data.playerhealth - playerInfo.playerhealth;
    var VPower = clientInfo.data.playerpower - playerInfo.playerpower;
    var VIntelligence = clientInfo.data.playerintelligence - playerInfo.playerintelligence;
    var VGay = clientInfo.data.playergay - playerInfo.playergay;
    var VNc = clientInfo.data.playernc - playerInfo.playernc;
    var playerpoint;
    if (playerInfo.playerpoint < VHealth + VPower + VIntelligence + VGay + VNc) {
        var protocolInfo = {protocol: clientInfo.protocol, data: {result: 3, msg: '加点数大于玩家所拥有的点数，有作弊嫌疑'}};
        sendData(socket, protocolInfo);
        return;
    }
    playerpoint = playerInfo.playerpoint - VHealth - VPower - VIntelligence - VGay - VNc;
    var update = 'update player set playerhealth=' + clientInfo.data.playerhealth + ',playerpower=' + clientInfo.data.playerpower + ',playerintelligence=' + clientInfo.data.playerintelligence + ',playergay=' + clientInfo.data.playergay + ',playernc=' + clientInfo.data.playernc + ',playerpoint=' + playerpoint + ' where playerid="' + clientInfo.data.playerid + '"';
    connection.query(update, function (err, res) {
        if (err) {
            console.log('update err in 10004:' + err);
            var protocolInfo = {protocol: clientInfo.protocol, data: {result: 0, msg: '更新属性失败'}};
            sendData(socket, protocolInfo);
            return;
        }
        var protocolInfo = {protocol: clientInfo.protocol, data: {result: 1, msg: '更新属性成功'}};
        sendData(socket, protocolInfo);
    });
}
function pro10005(connection, socket, clientInfo) {
    var select = 'select items from backpack where id=' + clientInfo.data.playerid + '" limit 1';
    connection.query(select, function (err, rows) {
        if (err) {
            console.log('select err in pro10005:' + err);
            var protocolInfo = {protocol: clientInfo.protocol, data: {result: 0, msg: '获取背包列表失败'}};
            sendData(socket, protocolInfo);
            return;
        }
        var protocolInfo = {protocol: clientInfo.protocol, data: {result: 1, msg: '获取背包列表成功', items: rows[0]}};
        sendData(socket, protocolInfo);
    });
}
function getPlayer(connection, id) {
    var select = 'select * from player where playerid="' + id + '" limit 1';
    connection.query(select, function (err, rows) {
        if (err) {
            console.log('select err in getPlayerInfo:' + err);
            return null;
        }
        return rows[0];
    });
}
function sendData(socket, data) {
    var json = JSON.stringify(data);
    socket.send(json);
}
exports.handleProtocol = handleProtocol;